package com.atcumt.QiAnXin;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/* 
 * 2
 * 12 A tidy tiger tied a tie tighter to tidy her tiny tail
 * 1 a
 * 16 A big black bug bit a big black bear made the big black bear bleed blood
 * 2 b?? b???
 */

public class Main01 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        in.nextLine();
        while (in.hasNext()) {
            String[] strs = in.nextLine().split(" ");
            String[] stopstr = in.nextLine().split(" ");
            int num = Integer.valueOf(strs[0]);
            Map<String, Integer> map = new HashMap<>();
            for (int i = 0; i < num + 1; i++) {
                if (!isStop(strs[i], stopstr)) {
                    if (map.containsKey(strs[i])) {
                        int n = map.get(strs[i]);
                        n++;
                        map.put(strs[i], n);
                    } else {
                        map.put(strs[i], 1);
                    }
                }
            }
            int res = 0;
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                res = Math.max(res, entry.getValue());
            }
            System.out.println(res);
        }
        in.close();
    }

    private static boolean isStop(String s, String[] str) {
        int n = Integer.valueOf(str[0]);
        for (int i = 0; i <= n; i++) {
            if (isMatch(s, str[i])) {
                return true;
            }
        }
        return false;
    }

    private static boolean isMatch(String s1, String s2) {
        String ss1 = s1.toLowerCase();
        String ss2 = s2.toLowerCase();
        if (ss1.length() != ss2.length()) {
            return false;
        }
        for (int i = 0; i < s1.length(); i++) {
            if (ss2.charAt(i) == '?') {
                continue;
            } else if (ss1.charAt(i) != ss2.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}
